package com.dragee.modules.work.service.impl;

import com.alibaba.excel.EasyExcel;
import com.dragee.modules.base.entity.DicEntity;
import com.dragee.modules.base.service.DicService;
import com.dragee.modules.work.service.ExcelService;
import com.dragee.utils.excel.CommonListener;
import com.dragee.utils.excel.ExcelDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("excelService")
public class ExcelServiceImpl implements ExcelService {
    @Autowired
    private DicService dicService;



//    @Override
//    public R<Object> importData(MultipartFile file) throws IOException {
//        //填充对应词典
//        Map<String,Map<String,Object>> map = new HashMap<>();
//        map.put(DictCodeConstants.SEX,this.getDictItemKey(DictCodeConstants.SEX));
//        map.put(DictCodeConstants.USER_POST,this.getDictItemKey(DictCodeConstants.USER_POST));
//        map.put(DictCodeConstants.USER_CLASSIFY,this.getDictItemKey(DictCodeConstants.USER_CLASSIFY));
//        map.put(DictCodeConstants.F_STATE,this.getDictItemKey(DictCodeConstants.F_STATE));
//        map.put("role",sysRoleService.getRoleNameMap());
//        //解析器
//        CommonListener<UserModel> commonListener = new CommonListener<>(map, UserModel.class,this::excelBatch);
//        EasyExcel.read(file.getInputStream()).head(UserModel.class).registerReadListener(commonListener).sheet().doRead();
//        List<String> msgList = commonListener.getMsgList();
//        if(msgList.size()>0){
//            return R.failed(StringUtils.join(msgList,","));
//        }
//        return R.ok();
//    }
//
//    @Override
//    public void exportData(HttpServletResponse response, UserDTO dto, int type) throws Exception {
//        List<UserModel> list = new ArrayList<>();
//        Map<Integer,String[]> dropDownMap = new HashMap<>(4);
//        if(type == 1) {
//            //查询需导出数据
//            list = this.baseMapper.getExcelList(dto);
//        }else {
//            //填充下拉框
//            dropDownMap.put(2,this.getDictItem(DictCodeConstants.SEX));
//            dropDownMap.put(5,this.getDictItem(DictCodeConstants.USER_POST));
//            dropDownMap.put(6,sysRoleService.getRoleName());
//            dropDownMap.put(7,this.getDictItem(DictCodeConstants.USER_CLASSIFY));
//            dropDownMap.put(8,this.getDictItem(DictCodeConstants.F_STATE));
//        }
//        ExcelUtil.writeExcel(response,list, String.valueOf(System.currentTimeMillis()), "科室人员", dropDownMap, UserModel.class);
//    }

    @Override
    public boolean importDemo(MultipartFile file) throws IOException {
        //填充对应词典
        Map<String, Map<String,Object>> map = new HashMap<>();
        map.put("sex", this.getDictItemKey("sex"));
        //解析器
        CommonListener<ExcelDemo> commonListener = new CommonListener<ExcelDemo>(map, ExcelDemo.class,this::excelBatch);
        EasyExcel.read(file.getInputStream())
                .head(ExcelDemo.class)
                .registerReadListener(commonListener)
                .sheet()
                .doRead();
        List<String> msgList = commonListener.getMsgList();
        if(msgList.size()>0){
            // return R.failed(StringUtils.join(msgList,","));
            return false;
        }
        return true;
    }






    /**
     * 获取下拉框字典
     * @param type 字典code
     * @return  String[]
     */
    private String[] getDictItem(String type){
        List<DicEntity> dictByType = dicService.findDic(type);
        if(null!=dictByType){
            return dictByType.stream().map(DicEntity::getChName).toArray(String[]::new);
        }
        return null;
    }

    /**
     * 获取字典 -name，key
     * @param type 字典code
     * @return  List<String>
     */
    private Map<String,Object> getDictItemKey(String type) {
        List<DicEntity> dictByType = dicService.findDic(type);
        if (null != dictByType) {
            return dictByType.stream().collect(Collectors.toMap(DicEntity::getChName, DicEntity::getId));
        }
        return new HashMap<>(4);
    }

    /** 执行数据库操作的地方
     * @param userList
     * @return
     */
    private boolean excelBatch (Collection<ExcelDemo> userList) {
        //数据处理及批量添加
        //返回提示
//        String  returnMsg= "";
//        if(b.length()>0){
//            b.append("表格存在重复数据：");
//            returnMsg=b.toString();
//        }
//        if(repeatData.size()>0){
//            returnMsg+="数据库已存在数据：";
//            returnMsg+=StringUtils.join(repeatData,",");
//        }
//        if(returnMsg.length()>0){
//            throw new CommonException(returnMsg);
//        }
        return true;
    }
}